package cn.fl.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysRegionService;
import net.sf.json.JSONArray;

/**
 * @Description: 地区表 控制层
 */
@Controller
@RequestMapping("/system/sysRegion")
public class SysRegionController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysRegionController.class);

	/**
	 * SysRegion 服务层BEAN
	 */
	@Autowired
	private ISysRegionService sysRegionService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toListPage", "跳转到区域下拉框页面.", CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysRegion/region");
		return view;
	}
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toTestPage.do", method = { RequestMethod.GET})
	public ModelAndView toTestPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toTestPage", "跳转到区域下拉框引用测试页面.", CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysRegion/regionQuote");
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("system/sysRegion/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 获取省级区域数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getProvinceRegion.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object getProvinceRegion(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		
		try {
			
			List<KeyValueVO> provinceKVList = this.sysRegionService.searchProvinceKVList();
			if(CollectionUtils.isEmpty(provinceKVList)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在！");
				BaseLogUtils.info(logger, "getProvinceRegion", "获取数据,数据不存在.", CurrentThreadContext.getCurrentUserName());
			}
			JSONArray provinceKVListJson = JSONArray.fromObject(provinceKVList.toArray());
			result.setData(provinceKVListJson);
			BaseLogUtils.info(logger, "getProvinceRegion", "获取数据.", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getProvinceRegion", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getProvinceRegion", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 根据区域代码获取子级区域数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRegionByRgnCd.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object getRegionByRgnCd(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String rgnCd = request.getParameter("rgnCd");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isBlank(rgnCd)){
			
		}
		try {
			
			List<KeyValueVO> rgnKvList = this.sysRegionService.searchRegineKVList(rgnCd);
			if(CollectionUtils.isNotEmpty(rgnKvList)){
				JSONArray rgnKvListJson = JSONArray.fromObject(rgnKvList.toArray());
				result.setData(rgnKvListJson);
				BaseLogUtils.info(logger, "getRegionByRgnCd", "获取数据.", CurrentThreadContext.getCurrentUserName());
			}
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getRegionByRgnCd", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getRegionByRgnCd", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

